package com.yms.controller;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.yms.pojo.MarriedPerson;
import com.yms.pojo.PageResult;
import com.yms.pojo.Result;
import com.yms.service.IMarriedPersonService;
import com.yms.util.AliPhoneUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-03-20
 */
@Controller
@RequestMapping("/marriedPerson")
public class MarriedPersonController {
    @Autowired
    private IMarriedPersonService marriedPersonService;
    @RequestMapping("showMarried")
    @ResponseBody
    public PageResult<MarriedPerson> showMarried(MarriedPerson searchCondition){
        return marriedPersonService.selectMarried(searchCondition);
    }
    //门户页面登录功能，确定要返回给前端的是一个登录成功的对象
    @RequestMapping("personLogin")
    @ResponseBody
    public MarriedPerson personLogin(MarriedPerson marriedPerson,String remenberMe){
        //根据获取的remenberMe来判断用户是否点击了记住我的功能
       return  marriedPersonService.personLogin(marriedPerson);
    }
    //新人注册功能实现
    @RequestMapping("reg")
    @ResponseBody
    public Result reg(MarriedPerson marriedPerson,HttpSession session,String smsCode){
        if(session.getAttribute("code").equals(smsCode)){
            return marriedPersonService.reg(marriedPerson);
        }else{
            return new Result(false,"短信验证码不匹配");
        }
    }
    //阿里云短信验证功能的实现
    @ResponseBody
    @RequestMapping("personCode")
    public String personCode(String phonenumber, HttpSession session) throws ClientException {
        AliPhoneUtil.setNewCode();
        //获取短信验证码
        Integer newCode = AliPhoneUtil.getNewCode();
        //将短信验证码存储在session对象中，为了在登录的时候进行比对
        session.setAttribute("code",newCode);
        //将短信发送给手机客户端
        SendSmsResponse sendSmsResponse = AliPhoneUtil.sendSms(phonenumber, newCode);
        return sendSmsResponse.getCode();
    }

}

